package xdTool

import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdController

class XdTableCfgController extends XdController{

    def index() {}

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        def result = ResultJson.resultJson()
        try {
            def widgetPanelId = Long.parseLong(params?.widgetPanelId)
            XdWidgetPanel panel = XdWidgetPanel.get(widgetPanelId)
            if(!panel){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                XdTableCfg xdTableCfg = XdTableCfg.findByDomId("dom-${panel.label}")
                if(!xdTableCfg){
                    xdTableCfg = XdTableCfg.newInstance()
                    xdTableCfg.setCreator(params.userNo)
                }
                xdTableCfg.setProperty("widgetPanelId",widgetPanelId)
                xdTableCfg.setUrl("../xdWidgetPanel/queryPanelTableData?id=${widgetPanelId}")
                xdTableCfg.setProperty("cellMinWidth",params.cellMinWidth?:"60")
                xdTableCfg.setProperty("size_",params.size_?:"")
                xdTableCfg.setProperty("even",params.even?:"")
                xdTableCfg.setProperty("page",params.page?:"")
                xdTableCfg.setProperty("skin",params?.skin )
                xdTableCfg.setProperty("toolbar",SysCommUtil.String2Clob(params.toolbar?:"false"))
                xdTableCfg.setProperty("height",params?.height)
                xdTableCfg.setProperty("width",params?.width)
                xdTableCfg.setProperty("domId","dom-${panel.label}")
                xdTableCfg.setProperty("elem","#dom-${panel.label}")
                xdTableCfg.setProperty("title",panel.name)
                xdTableCfg.setProperty("updater",params.updater)
                xdTableCfg.setProperty("queryFlag",params.queryFlag?:"")
                xdTableCfg.setProperty("autoFlag",params.autoFlag?:"")
                xdTableCfg.setProperty("autoInterval",params.autoInterval?:"")

                xdTableCfg.setProperty("totalRow",params.totalRow?:"false")

                xdTableCfg.save(flush: true)

                panel.setDataId(xdTableCfg.id)
                panel.save(flush: true)

                result.put(ResultJson.DATA.name,[dataId:xdTableCfg.id])
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def update() {
        return null
    }

    @Override
    def add() {
        return null
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        return null
    }

    @Override
    def query() {
        return null
    }

    @Override
    def delete() {
        return null
    }
}
